package com.group.controller;


import com.group.entity.ProjectOutside;
import com.group.entity.User;
import com.group.service.ProjectOutsideService;
import com.group.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Controller
//@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    ProjectOutsideService projectOutsideService;

    @GetMapping("/{url}")
    public String redirect(@PathVariable("url") String url) {
        return url;
    }

    @RequestMapping("/myproject")
    public String myproject() {
        return "myproject";
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }


    @RequestMapping("/login")
    public String login(String userName, String passWord, Model model) {
        //获得当前的用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(userName, passWord);
        //执行登陆方法，如果没异常，则ok
        try {
            subject.login(token);
            return "index2";
        } catch (UnknownAccountException e) {//用户名不存在
            model.addAttribute("msg", "用户名错误");
            return "login";
        } catch (IncorrectCredentialsException ice) {//密码错误
            model.addAttribute("msg", "密码错误");
            return "login";
        }

    }

    @RequestMapping("/registerUser")
    public String register(String userName, String passWord, String personalName, Model model) {
        boolean result = false;
        try {
            User user = new User();
            user.setUserName(userName);
            user.setPassWord(passWord);
            user.setPersonalName(personalName);
            result = userService.save(user);
        } catch (Exception exception) {
            model.addAttribute("error", "用户名:" + userName + "已经存在!");
            return "register";
        }
        if (result) {
            return "login";
        }
        return "register";

    }

    @RequestMapping("/checkbox")
    @ResponseBody
    public String checkbox(HttpServletRequest request, @RequestParam("interest") String[] interest) {
        StringBuffer sb = new StringBuffer();
        List<ProjectOutside> projectOutsides = null;
        if(interest.length == 1) {
         projectOutsides = projectOutsideService.find(interest[0],null);
        }
        if (interest.length == 2){
            projectOutsides = projectOutsideService.find(interest[0],interest[1]);
        }

        projectOutsides.forEach(System.out::println);
        return "ok";
    }

    @RequestMapping("/lookprojectsByTags")
    @ResponseBody
    public List<ProjectOutside> lookProjectsByTags(HttpServletRequest request, @RequestParam("zone") String[] zone) {
        StringBuffer sb = new StringBuffer();
        List<ProjectOutside> projectOutsides = null;
        if(zone.length == 1) {
         projectOutsides = projectOutsideService.find(zone[0],null);
        }
        if (zone.length == 2){
            projectOutsides = projectOutsideService.find(zone[0],zone[1]);
        }
        if(zone.length == 3) {
//            projectOutsides = projectOutsideService.find(zone[0],zone[1],zone[2]);
        }
        projectOutsides.forEach(System.out::println);
        return projectOutsides;
    }
}

